package com.ccjk.beusoft.thirdlib.plugin3;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Build;
import android.text.TextUtils;
import android.widget.Toast;

/**
 * Created by CCJK on 2017/12/2.
 */

public class ActivityProxy {
    private Activity proxyActivity;
    private PluginPackage pluginPackage;
    private String pluginActivityName;
    private ActivityInfo activityInfo;
    private PluginActivityInterface pluginActivity;
    private Resources.Theme pluginActivityTheme;

    public ActivityProxy(Activity proxyActivity) {
        this.proxyActivity = proxyActivity;
    }

    public void onCreate(Intent intent) {
        String pluginPackageName = intent.getStringExtra(Const.KEY_PLUGIN_PACKAGE_NAME);
        PluginPackage tempPackage = PluginManager.getInstance(proxyActivity).getPluginPackage(pluginPackageName);
        if (tempPackage == null) {
            Toast.makeText(proxyActivity, "插件包不存在", Toast.LENGTH_SHORT).show();
            return;
        }
        pluginPackage = tempPackage;
        pluginActivityName = intent.getStringExtra(Const.KEY_PLUGIN_ACTIVITY_NAME);
        if (pluginActivityName == null) {
            pluginActivityName = pluginPackage.defaultActivityClassName;
        }

        for (ActivityInfo info : pluginPackage.packageInfo.activities) {
            if (TextUtils.equals(info.name, pluginActivityName)) {
                activityInfo = info;
                int defaultTheme=pluginPackage.packageInfo.applicationInfo.theme;
                if(activityInfo.theme==0){
                    if(defaultTheme!=0){
                        activityInfo.theme=defaultTheme;
                    }else {
                        if (Build.VERSION.SDK_INT >= 14) {
                            activityInfo.theme = android.R.style.Theme_DeviceDefault;
                        } else {
                            activityInfo.theme = android.R.style.Theme;
                        }
                    }
                }
                break;
            }

        }
        if (activityInfo == null) {
            Toast.makeText(proxyActivity, "插件不存在該類", Toast.LENGTH_SHORT).show();
            return;
        }

        if(activityInfo.theme>0){
            proxyActivity.setTheme(activityInfo.theme);
        }
        Resources.Theme superThem=proxyActivity.getTheme();
        pluginActivityTheme=pluginPackage.resources.newTheme();
        pluginActivityTheme.setTo(superThem);
        try {
            pluginActivityTheme.applyStyle(activityInfo.theme,true);
        }catch (Exception e){
            e.printStackTrace();
        }

        Util.startActivity(proxyActivity, pluginPackage, pluginActivityName);
    }



    public ClassLoader getPluginClassLoader() {
        if(pluginPackage==null) return null;
        return pluginPackage.pluginClassLoader;
    }

    public AssetManager getPluginAssetManager() {
        return pluginPackage.assetManager;
    }

    public Resources getPluginResource() {
        if(pluginPackage==null) return null;
        return pluginPackage.resources;
    }

    public Resources.Theme getTheme(){
        return pluginActivityTheme;
    }
}
